package com.mobile.collect.activitys;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobile.collect.R;
import com.mobile.collect.beans.DevicePicInfo;
import com.mobile.collect.beans.DevicePointInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.dialogs.BottomListDialog;
import com.mobile.collect.dialogs.v3.TipDialog;
import com.mobile.collect.interfaces.PopWindowInterface;
import com.mobile.collect.present.PicPointPresent;
import com.mobile.collect.view.FloorPop;
import com.mobile.collect.view.InputDialog;
import com.mobile.collect.view.PopWindowEditText;
import com.mobile.collect.view.PopWindowTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.mvp.XActivity;
@Deprecated
public class AddDevicePicPointActivity2 extends XActivity<PicPointPresent> {

    boolean isUpdate;
    Long picDbId;
    Long pointDbId;
    DevicePointInfo targetInfo;
//    @BindView(R.id.nameTv)
//    EditText nameTv;
    @BindView(R.id.locationNoEt)
    EditText locationNoEt;
//    @BindView(R.id.expandEt)
//    TextView expandEt;
    @BindView(R.id.zhijingEt)
    TextView zhijingEt;
    @BindView(R.id.distanceEt)
    TextView distanceEt;
    @BindView(R.id.heightEt)
    TextView heightEt;
    @BindView(R.id.countEt)
    TextView countEt;
//    @BindView(R.id.typeTv)
//    PopWindowTextView typeTv;
//    @BindView(R.id.subtypeTv)
//    PopWindowTextView subtypeTv;
    @BindView(R.id.hardTv)
    PopWindowTextView hardTv;
    @BindView(R.id.hardeasonTv)
    PopWindowEditText hardeasonTv;
    @BindView(R.id.dangerTv)
    PopWindowTextView dangerTv;
    @BindView(R.id.dangerReasonTv)
    PopWindowEditText dangerReasonTv;
    @BindView(R.id.keepWarmTv)
    PopWindowTextView keepWarmTv;
    @BindView(R.id.descTv)
    PopWindowEditText descTv;
    @BindView(R.id.box_hard_remark)
    LinearLayout box_hard_remark;
    @BindView(R.id.box_danger_remark)
    LinearLayout box_danger_remark;
    @BindView(R.id.countLayout)
    LinearLayout countLayout;
    DevicePicInfo picInfo;


    @Override
    public int getContentViewId() {
        return R.layout.activity_adddevicepicpoint;
    }

    @Override
    public void initView() {
        super.initView();
        if (isUpdate) {
            titleTv.setText("修改点");
        } else {
            titleTv.setText("新增点");
        }
        locationNoEt.setSelection("NA".length());
//        typeTv.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                String inputText = s.toString();
//                ArrayList<String> list = getP().getChildTypeList(inputText);
//                if (!list.isEmpty()) {
//                    subtypeTv.setText(list.get(0));
//                    subtypeTv.setMenuList(list);
//                }
//            }
//        });


//        subtypeTv.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                String inputText = s.toString();
//                if (TextUtils.equals(inputText, "空冷丝堵")) {
//                    countEt.setText("1");
//                    countLayout.setVisibility(View.VISIBLE);
//                } else {
//                    countLayout.setVisibility(View.GONE);
//                    countEt.setText("");
//                }
//            }
//        });

        //高度大于2米自动难于检测
        heightEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    String inputStr = s.toString();
                    if (isNull(inputStr)) {
                        hardTv.setText("否");
                    } else {
                        Double inputNumber = Double.parseDouble(inputStr);
                        if (inputNumber > 2) {
                            hardTv.setText("是");
                        } else {
                            hardTv.setText("否");
                        }
                    }
                } catch (Exception e) {
                }
            }
        });


        //险于检测
        dangerTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    String inputStr = s.toString();
                    if (isNull(inputStr)) {
                        box_danger_remark.setVisibility(View.GONE);
                    } else {
                        if (inputStr.equals("是")) {
                            box_danger_remark.setVisibility(View.VISIBLE);
                        } else {
                            box_danger_remark.setVisibility(View.GONE);
                            dangerReasonTv.setText("");

                        }
                    }
                } catch (Exception e) {
                }
            }
        });

        hardTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    String inputStr = s.toString();
                    if (isNull(inputStr)) {
                        box_hard_remark.setVisibility(View.GONE);
                    } else {
                        if (inputStr.equals("是")) {
                            box_hard_remark.setVisibility(View.VISIBLE);
                        } else {
                            box_hard_remark.setVisibility(View.GONE);
                            hardeasonTv.setText("");
                        }
                    }
                } catch (Exception e) {
                }
            }
        });


        descTv.setMenuList(getP().getAllDescription());
    }

    private void setDefault() {
        if (picDbId == -1) {
            return;
        }
        targetInfo = EntityManager.getInstance().queryDevicePointInfoIById(pointDbId);
        if (null != targetInfo) {
            setPointInfo();
        } else {
//            nameTv.setText(picInfo.getReference());
        }
    }


    private void setPointInfo() {
//        nameTv.setText(targetInfo.getDeviceName());
        locationNoEt.setText(targetInfo.getAddressNo());
//        expandEt.setText(targetInfo.getExpendNo());
        zhijingEt.setText(targetInfo.getDiameter());
        distanceEt.setText(targetInfo.getDistence());
        heightEt.setText(targetInfo.getHeight());
//        typeTv.setText(targetInfo.getType());
//        subtypeTv.setText(targetInfo.getChildType());

        String isYes = targetInfo.getCheckHardType();

        hardTv.setText(targetInfo.getCheckHardType());
        if (TextUtils.equals("是", isYes)) {
            hardeasonTv.setVisibility(View.VISIBLE);
        }
        hardeasonTv.setText(targetInfo.getCheckHardReason());
        isYes = targetInfo.getDangerCheck();
        dangerTv.setText(targetInfo.getDangerCheck());
        if (TextUtils.equals("是", isYes)) {
            dangerReasonTv.setVisibility(View.VISIBLE);
        }

        dangerReasonTv.setText(targetInfo.getDangerCheckReaseon());
        keepWarmTv.setText(targetInfo.getHeatPreservation());
        descTv.setText(targetInfo.getAdditionalDes());
        if (!isUpdate) {
            targetInfo.set_ID(null);
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        picInfo = EntityManager.getInstance().queryDevicePicIById(picDbId);
        setDefault();
    }

    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        save();
    }

    @Override
    public void getDataFromServer() {

    }


    @Override
    public void getIntentData() {
        picDbId = getIntent().getLongExtra("picdbid", -1);
        pointDbId = getIntent().getLongExtra("pointdbid", -1);
        isUpdate = getIntent().getBooleanExtra("isupdate", false);
    }

    @Override
    public PicPointPresent newP() {
        return new PicPointPresent();
    }


    public static void lunch(Activity context, boolean isUpdate, Long picDbId, Long pointDbId) {
        Intent intent = new Intent(context, AddDevicePicPointActivity2.class);
        intent.putExtra("picdbid", picDbId);
        intent.putExtra("isupdate", isUpdate);
        intent.putExtra("pointdbid", pointDbId);
        context.startActivityForResult(intent, 0x001);
    }


    protected void showBottomDialog(final TextView view, List<String> dicList) {
        BottomListDialog bottomListDialog = new BottomListDialog(this, new BottomListDialog.BottomListSelectListener() {
            @Override
            public void itemClickCallback(String selectStr) {
                view.setText(selectStr);
                setReleven(view.getId(), selectStr);
            }
        });
        bottomListDialog.disPlay(dicList);
    }

    private void setReleven(int tvId, String txt) {
        switch (tvId) {
//            case R.id.nameTv:
//                List<String> list = getP().getChildTypeList(nameTv.getText().toString());
//                if (list.size() > 0) {
//                    subtypeTv.setText(list.get(0));
//                }
//                break;
//            case R.id.typeTv:
//                List<String> sublist = getP().getChildTypeList(typeTv.getText().toString());
//                if (sublist.size() > 0) {
//                    subtypeTv.setText(sublist.get(0));
//                }
            case R.id.hardTv:
                if (txt.equals("是")) {
                    box_hard_remark.setVisibility(View.VISIBLE);
                    dangerReasonTv.setText(getP().getHardReasonList().get(0));

                } else {
                    box_hard_remark.setVisibility(View.GONE);
                    hardeasonTv.setText("");
                }
                break;
            case R.id.dangerTv:
                if (txt.equals("是")) {
                    dangerReasonTv.setText(getP().getDangerReasonList().get(0));
                    box_danger_remark.setVisibility(View.VISIBLE);
                } else {
                    box_danger_remark.setVisibility(View.GONE);


                }
                break;
        }
    }


    @OnClick({R.id.rightIv, R.id.expandEt, R.id.zhijingEt, R.id.distanceEt, R.id.heightEt, R.id.countEt})
    public void onViewClicked(View view) {
        List<String> list = new ArrayList<>();
        TextView valueTv = null;
        switch (view.getId()) {
            case R.id.rightIv:
                Long deviceDbId = picInfo.getParentId();
                CopyPicListActivity.lunch(this, deviceDbId, picDbId, true);
                return;
            case R.id.expandEt:
//                showFloorPop("扩展号", expandEt);
                break;
            case R.id.zhijingEt:
                showFloorPop("直径", zhijingEt);
                break;
            case R.id.distanceEt:
                showFloorPop("距离", distanceEt);
                break;
            case R.id.heightEt:
                showFloorPop("高度", heightEt);
                break;
            case R.id.countEt:
                showFloorPop("数量", countEt);
                break;

        }
    }


    @Override
    public int getTitleLayoutResId() {
        return 0;
    }

    public void save() {
//        String name = nameTv.getText().toString();
//        if (TextUtils.isEmpty(name)) {
//            TipDialog.show(this, "请输入设备名称", TipDialog.TYPE.WARNING);
//            return;
//        }
        String location = locationNoEt.getText().toString();
        if (TextUtils.isEmpty(location)) {
            TipDialog.show(this, "请输入位号", TipDialog.TYPE.WARNING);
            return;
        }

//        String expandNo = expandEt.getText().toString();
//        if (TextUtils.isEmpty(expandNo)) {
//            TipDialog.show(this, "请输入扩展号", TipDialog.TYPE.WARNING);
//            return;
//        }

//        if (expandNo.length() != 3) {
//            TipDialog.show(this, "请输入3位数扩展号", TipDialog.TYPE.WARNING);
//            return;
//        } else if (!isUpdate) {
//            long count = EntityManager.getInstance().queryDevicePointByName(picDbId, expandNo);
//            if (count != 0) {
//                TipDialog.show(this, "扩展号已存在", TipDialog.TYPE.WARNING);
//                return;
//            }
//        }

        String zhijing = zhijingEt.getText().toString();
        if (TextUtils.isEmpty(zhijing)) {
            TipDialog.show(this, "请输入直径", TipDialog.TYPE.WARNING);
            return;
        }
        String distance = distanceEt.getText().toString();
        if (TextUtils.isEmpty(distance)) {
            TipDialog.show(this, "请输入距离", TipDialog.TYPE.WARNING);
            return;
        }
        String height = heightEt.getText().toString();
        if (TextUtils.isEmpty(height)) {
            TipDialog.show(this, "请输入高度", TipDialog.TYPE.WARNING);
            return;
        }
//        String type = typeTv.getText().toString();
//        if (TextUtils.isEmpty(type)) {
//            TipDialog.show(this, "请输入组件类型", TipDialog.TYPE.WARNING);
//            return;
//        }
//        String subType = subtypeTv.getText().toString();
//        String count = countEt.getText().toString();
//        int deviceCount = 0;
//        if (TextUtils.isEmpty(subType)) {
//            TipDialog.show(this, "请输入子组件类型", TipDialog.TYPE.WARNING);
//            return;
//        } else if (TextUtils.equals("空冷丝堵", subType)) {
//            if (TextUtils.isEmpty(count)) {
//                TipDialog.show(this, "组件数量不能为空", TipDialog.TYPE.WARNING);
//                return;
//            }
//            if (Integer.parseInt(count) <= 0) {
//                TipDialog.show(this, "组件数量需大于0", TipDialog.TYPE.WARNING);
//                return;
//            }
//            deviceCount = Integer.parseInt(count);
//        }


        String desc = descTv.getText().toString();
        if (TextUtils.isEmpty(desc)) {
            TipDialog.show(this, "请输入描述内容", TipDialog.TYPE.WARNING);
            return;
        }


        String hard = hardTv.getText().toString();
        String hardreason = hardeasonTv.getText().toString();

        if (hard.equals("是") && TextUtils.isEmpty(hardreason)) {
            TipDialog.show(this, "请输入难于检测原因", TipDialog.TYPE.WARNING);
            return;
        }
        String dangerCheck = dangerTv.getText().toString();
        String dangerReason = dangerReasonTv.getText().toString();

        if (dangerCheck.equals("是") && TextUtils.isEmpty(dangerReason)) {
            TipDialog.show(this, "请输入险于检测原因", TipDialog.TYPE.WARNING);
            return;
        }

//        String label = getP().getPointName(expandNo, type);
//        if (deviceCount != 0) {
//            label = label + "\n" + "1-" + deviceCount;
//        }
        DevicePointInfo info = targetInfo;
        if (null == info) {
            info = new DevicePointInfo();
            info.setPointDircetion("up");
            info.setDirection(90);
        }
        if (!isUpdate) {
            info.setScaleY(0.4f);
            info.setScaleX(0.4f);
        }

//        info.setDeviceName(name);
        info.setAdditionalDes(desc);
        info.setAddressNo(location);
        info.setCheckHardType(hard);
        info.setCheckHardReason(hardreason);
//        info.setChildType(subType);
//        info.setDeviceCount(deviceCount);
//        info.setType(type);
        info.setDangerCheck(dangerCheck);
        info.setDangerCheckReaseon(dangerReason);
        info.setDistence(distance);
        info.setHeight(height);
//        info.setExpendNo(expandNo);
        info.setDiameter(zhijing);
        info.setHeatPreservation(keepWarmTv.getText().toString());
        info.setParentId(picDbId);
//        info.setLabelName(label);


        Long id = EntityManager.getInstance().savePointInfo(info);
        info.set_ID(id);

        Intent intent = new Intent();
//        intent.put("devicepointinfo", info);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == 0x001) {
            Long copyPointDbId = data.getLongExtra("copypointdbid", -1);
            DevicePointInfo pointInfo = EntityManager.getInstance().queryDevicePointInfoIById(copyPointDbId);
            if (isUpdate) {
                pointInfo.set_ID(targetInfo.get_ID());
                pointInfo.setParentId(targetInfo.getParentId());
                targetInfo = pointInfo;
            } else {
                targetInfo.set_ID(null);
                targetInfo = pointInfo;
            }
            setPointInfo();
        }
    }


    public void OnMoreClick(View view) {
        ViewGroup box = (ViewGroup) view.getParent();
        PopWindowInterface editText = null;
        for (int i = 0; i < box.getChildCount(); i++) {
            if (box.getChildAt(i) instanceof PopWindowInterface) {
                editText = (PopWindowInterface) box.getChildAt(i);
            }
        }
        if (editText != null) {
            if (editText instanceof EditText) {
                showIME(false, (EditText) editText);
            }
            editText.showMenu();
        }
    }

    //软键盘打开与收起
    public void showIME(boolean show, EditText editText) {
        if (editText == null) {
            return;
        }
        if (show) {
            editText.requestFocus();
            editText.setFocusableInTouchMode(true);
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
        } else {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

    private void showFloorPop(String label, final TextView clickView) {
        FloorPop keyBroadPop = new FloorPop(this, clickView, label, new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                clickView.setText(value);
            }
        },0);
        keyBroadPop.show();
    }

}
